package com.medusa.aps.business.modules.plan.mp.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.aps.business.model.enums.SchedulingStatusEnum;
import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.demand.model.input.ControlDetailParam;
import com.medusa.aps.business.modules.demand.model.output.ScheduleDetailSelfModel;
import com.medusa.aps.business.modules.plan.model.input.SuggestedPlanListParam;
import com.medusa.aps.business.modules.plan.mp.entity.SuggestedPlan;
import com.medusa.aps.business.modules.plan.mp.mapper.SuggestedPlanMapper;
import com.medusa.aps.business.modules.plan.mp.service.SuggestedPlanService;
import org.springframework.stereotype.Service;

import java.time.LocalTime;

/**
 * 建议计划服务实现类
 *
 * @author yuxh
 * @since 2023-08-14 17:37:50
 */
@Service("suggestedPlanService")
public class SuggestedPlanServiceImpl extends ServiceImpl<SuggestedPlanMapper, SuggestedPlan> implements SuggestedPlanService {

    @Override
    public Page<SuggestedPlan> queryPage(SuggestedPlanListParam param) {

        LambdaQueryChainWrapper<SuggestedPlan> lambdaQuery = this.lambdaQuery()
                .like(StrUtil.isNotBlank(param.getOrderNumber()), SuggestedPlan::getOrderNumber, param.getOrderNumber())
                .like(StrUtil.isNotBlank(param.getMaterialCode()), SuggestedPlan::getMaterialCode, param.getMaterialCode())
                .like(StrUtil.isNotBlank(param.getMaterialName()), SuggestedPlan::getMaterialName, param.getMaterialName())
                .ne(SuggestedPlan::getSchedulingStatus, SchedulingStatusEnum.CLOSE)
                .in(CollUtil.isNotEmpty(param.getIds()), SuggestedPlan::getId, param.getIds());
        if (param.getOrderDeliveryTime().getStart() != null && param.getOrderDeliveryTime().getEnd() != null) {
            lambdaQuery.between(SuggestedPlan::getOrderDeliveryTime, param.getOrderDeliveryTime().getStart(), param.getOrderDeliveryTime().getEnd());
        }
        if (param.getPlanStartTime() != null) {
            lambdaQuery.between(SuggestedPlan::getPlanStartTime, param.getPlanStartTime().atStartOfDay(), param.getPlanStartTime().atTime(LocalTime.MAX));
        }
        if (param.getPlanCompletionTime() != null) {
            lambdaQuery.between(SuggestedPlan::getPlanCompletionTime, param.getPlanCompletionTime().atStartOfDay(), param.getPlanCompletionTime().atTime(LocalTime.MAX));
        }
        return lambdaQuery
                .orderByDesc(SuggestedPlan::getCreateTime)
                .page(new Page<>(param.getCurrent(), param.getSize()));
    }


    @Override
    public Page<ScheduleDetailSelfModel> selfControl(ControlDetailParam controlDetailParam, Page<ScheduleDetailSelfModel> page) {
        return baseMapper.selfControl(controlDetailParam, page);
    }
}

